/*
 * @Author: wangwei wwdqq7@qq.com
 * @Date: 2025-04-01 15:00:00
 * @LastEditors: wangwei wwdqq7@qq.com
 * @LastEditTime: 2025-04-01 15:00:00
 * @FilePath: /FullStack/pro/typeorm-mysql/src/auth/strategies/local.strategy.ts
 * @Description: 本地认证策略，用于验证用户名和密码
 */
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-local';

import { AuthService } from '@/auth/auth.service';

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
  constructor(private authService: AuthService) {
    super();
  }

  /**
   * 验证用户名和密码
   * @param username 用户名
   * @param password 密码
   * @returns 验证通过的用户信息
   */
  async validate(username: string, password: string): Promise<any> {
    const user = await this.authService.validateUser(username, password);
    if (!user) {
      throw new UnauthorizedException('用户名或密码不正确');
    }
    return user;
  }
}
